//
//  MainViewController.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/6.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit
private var OBJC_HEHE_KEY = UnsafePointer<Void>()
class MainViewController: UITabBarController {
    var loginState = UserAcount.loadUserData() != nil
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewController()
        // Do any additional setup after loading the view.
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setComposeBtn()
    }
    @objc private func clickComposeBtn() {
        if loginState {
           let composeView = DWDComposeView()
            composeView.show(self)
        }else {
        let vc = OAuthViewController()
        self.presentViewController(vc, animated: true, completion: nil)
        }
    }
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "clickComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
        self.tabBar.addSubview(btn)
        return btn
        }()
    private func setComposeBtn() {
        let count = self.childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
    }
    private func addChildViewController() {

        addChildViewController(HomeTableViewController(), "首页", "tabbar_home",0)
        addChildViewController(MessageTableViewController(), "消息", "tabbar_message_center",1)
        addChildViewController(UIViewController())
        addChildViewController(DisacoveryTableViewController(), "发现", "tabbar_discover",3)
        addChildViewController(ProfileTableViewController(), "我的", "tabbar_profile",4)
    
    }

    private  func addChildViewController(vc:UIViewController, _ title:String,_ image:String,_ index:Int = 0) {
        let dwdItem = DWDTabBarItem()
        vc.tabBarItem = dwdItem
        objc_setAssociatedObject(dwdItem, &OBJC_HEHE_KEY, NSNumber(integer: index), objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named:image)
//        vc.tabBarItem.selectedImage = UIImage(named: selectedImage)
        let nav = DWDBaseNavViewController(rootViewController: vc)
        addChildViewController(nav)

        
    }
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        var index = 0
    for childView in tabBar.subviews {
    if childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
    let idx = objc_getAssociatedObject(item, &OBJC_HEHE_KEY) as! Int
        if index == idx {
    for childBtn in childView.subviews {
    if childBtn.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
    childBtn.transform = CGAffineTransformMakeScale(0.5, 0.5)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            childBtn.transform = CGAffineTransformIdentity
            }, completion: { (finish) -> Void in

        })
            }
            }}
            index++
            }
        }
    }
}
